﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Payroll;
using System.Data.SqlServerCe;
using System.Data.SqlClient;

namespace Payroll
{
    public partial class Payemployee : System.Windows.Forms.Form { 
    
        public Payemployee()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)//小时工 每一周结算一次 截止日期在每周周五   正常员工 每月月底结算工资  销售员工 每两周结算一次 截止日期在第二周周五
        {
            DateTime startdate = Convert.ToDateTime(dateTimePicker1.Text);
            DateTime enddate = Convert.ToDateTime(dateTimePicker2.Text);

            if (startdate > enddate)
            {
                MessageBox.Show("开始日期应早于支付日期，请重新输入！","警告");
                return;
            }
            else
            {
                int id = int.Parse(textid.Text);
                PayrollDatabase database = new sytpayrolldatabase();
                Employee ee = database.GetEmployee(id);
                Paycheck st = new Paycheck(startdate, enddate);
                double pay = ee.Classification.CalculatePay(st);
                PaydayTransaction pt = new PaydayTransaction(enddate, database);
                pt.Execute();
                Paycheck pc = pt.GetPaycheck(id);

                if (pc != null)
                {
                    string payMethod = textmethod.Text;
                    database.pay(id, pay, enddate, payMethod);
                    if (MoneyBox.Text == "美元")
                        MessageBox.Show("本次共支付 美元：" + pay * 0.15 + " 元", "支付成功");
                    else if (MoneyBox.Text == "比特币")
                        MessageBox.Show("本次共支付 比特币：" + pay * 0.000004 + " 个", "支付成功");
                    else
                        MessageBox.Show("本次共支付 人民币：" + pay + " 元", "支付成功");

                    myFile.myWrite_operate(myUser.Name, "支付工资 id: " + id + "  金额: " + pay);
                }
                else
                {
                    MessageBox.Show("未到支付日期！", "支付失败");
                }
            }
        }

        private void MoneyBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (MoneyBox.Text == "美元")
                MessageBox.Show("1 rmb = 0.1544 dollor","汇率提示");
            else if (MoneyBox.Text == "比特币")
                MessageBox.Show("1 rmb = 0.000004 Bitcoin", "汇率提示");
        }

        private void Payemployee_Load(object sender, EventArgs e)
        {
            string sqlStr;
            string id;
            string connString = @"Data Source=..\..\Database1.sdf";
            SqlCeConnection connection = new SqlCeConnection(connString);
            connection.Open();
            sqlStr = " select id from Summarytable order by id";
            SqlCeCommand cmd = new SqlCeCommand(sqlStr, connection);
            SqlCeDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                id = Convert.ToString(reader[0]);
                Console.WriteLine(id);
                textid.Items.Add(id);
            }
            connection.Close();
        }
    }
}
